package com.shzu.jwc.ty.controller;

import com.shzu.jwc.ty.bean.PeExaminationAppointment;
import com.shzu.jwc.ty.common.ExportExcelUtils;
import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.common.Page;
import com.shzu.jwc.ty.common.Semester;
import com.shzu.jwc.ty.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/examinationscoreone")
@Controller
public class PeExaminationScoreOnecontroller {

	@Autowired
	SystemConfigservice systemConfigservice;
	
	@Autowired
	StudentInfoservice studentInfoservice;
	
	@Autowired
	PeExaminationScoreservice peExaminationScoreservice;
	
	@Autowired
	PeExaminationservice peExaminationservice;

	@Autowired
	SystemYearservice yearservice;

	@RequestMapping(value = "/main.action",method = RequestMethod.GET)
	public String showmain(Model model){
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("dates",yearservice.getSystemYear());
		model.addAttribute("code", systemConfigservice.getvalue("sportsskillsone"));
		model.addAttribute("name", peExaminationservice.getskilltest("体育技能一"));
		return "examinationscoreone/main";
	}

	@ResponseBody
	@RequestMapping(value = "/getData.action",method = RequestMethod.POST)
	public Map<String,Object> getData(@RequestParam("systemYearId") Integer systemYearId, @RequestParam("peExaminationAppointmentScore") Double peExaminationAppointmentScore, @RequestParam("peExaminationId") Integer peExaminationId, @RequestParam("userStudentInfoNum") String userStudentInfoNum){
		if(systemYearId==0){
			systemYearId=yearservice.getoneyearby(Semester.getnow());
		}
		if(userStudentInfoNum==""){
			userStudentInfoNum=null;
		}
		List<PeExaminationAppointment> data=peExaminationScoreservice.getData(systemYearId,peExaminationAppointmentScore,peExaminationId,userStudentInfoNum,1);
		Map<String,Object> maps=new HashMap<String,Object>();
		maps.put("data",data);
		return maps;
	}

	@ResponseBody
	@RequestMapping(value = "/updateMultScore.action",method = RequestMethod.POST)
	public Msg updateMultScore(@RequestParam("name") String name,@RequestParam("score") Double score,@RequestParam("peExaminationAppointmentId") Integer peExaminationAppointmentId){
		try{
			Double Totalscore=peExaminationScoreservice.updateMultscore(name,score,peExaminationAppointmentId);
			return Msg.success("操作成功").add("Totalscore",Totalscore);
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("操作失败");
		}

	}

	@RequestMapping("/newExport.action")
	public ModelAndView newExport(@RequestParam("systemYearId") Integer systemYearId, @RequestParam("peExaminationAppointmentScore") Double peExaminationAppointmentScore, @RequestParam("peExaminationId") Integer peExaminationId, @RequestParam("userStudentInfoNum") String userStudentInfoNum,HttpServletResponse response) {

		if(systemYearId==0){
			systemYearId=yearservice.getoneyearby(Semester.getnow());
		}
		if(userStudentInfoNum==""){
			userStudentInfoNum=null;
		}
		try {
			List<PeExaminationAppointment> data=peExaminationScoreservice.getData(systemYearId,peExaminationAppointmentScore,peExaminationId,userStudentInfoNum,1);
			String code = systemConfigservice.getvalue("sportsskillsone");
			String title = "体育技能一考试成绩";
			String[] rowsName = new String[]{"学年", "考试名称", "学号", "姓名", "入学时间", "所在学院", "课程", "课程代码", "考试成绩", "平时成绩", "期中成绩", "总成绩"};
			List<Object[]> dataList = new ArrayList<Object[]>();
			Object[] objs = null;
			for (int i = 0; i < data.size(); i++) {
				PeExaminationAppointment po = null;
				po = data.get(i);
				objs = new Object[rowsName.length];
				objs[0] = po.getSystemYear().getSystemYearName();
				objs[1] = po.getPeExamination().getPeExaminationName();
				objs[2] = po.getUserStudentInfo().getUserStudentInfoNum();
				objs[3] = po.getUserStudentInfo().getUserStudentInfoName();
				objs[4] = po.getUserStudentInfo().getUserStudentInfoYear();
				objs[5] = po.getUserStudentInfo().getUserStudentInfoCollege();
				objs[6] = "体育技能一考试";
				objs[7] = code;
				objs[8] = po.getPeExaminationAppointmentScore() == null ? "" : po.getPeExaminationAppointmentScore();
				objs[9] = po.getPeExaminationNormalscore() == null ? "" : po.getPeExaminationNormalscore();
				objs[10] = po.getPeExaminationMidscore() == null ? "" : po.getPeExaminationMidscore();
				objs[11] = po.getPeExaminationTotalscore() == null ? "" : po.getPeExaminationTotalscore();
				dataList.add(objs);
			}
			ExportExcelUtils ex = new ExportExcelUtils(title, rowsName, dataList, response);
			ex.exportData();
		}catch (Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/newExportJwc.action")
	public ModelAndView newExportJwc(@RequestParam("systemYearId") Integer systemYearId, @RequestParam("peExaminationAppointmentScore") Double peExaminationAppointmentScore, @RequestParam("peExaminationId") Integer peExaminationId, @RequestParam("userStudentInfoNum") String userStudentInfoNum,HttpServletResponse response) {

		if(systemYearId==0){
			systemYearId=yearservice.getoneyearby(Semester.getnow());
		}
		if(userStudentInfoNum==""){
			userStudentInfoNum=null;
		}
		try {
			List<PeExaminationAppointment> data=peExaminationScoreservice.getData(systemYearId,peExaminationAppointmentScore,peExaminationId,userStudentInfoNum,1);
			String code=systemConfigservice.getvalue("sportsskillsone");
			String title="体育技能一考试成绩";
			String[] rowsName=new String[] {"取得学年","取得学期","学号","课程／环节代码","课程类别一","课程类别二","课程类别三","环节类别","修读性质","考核方式","辅修标记","补考标记","特殊情况标记","成绩表现形式","原始成绩","授课方式","等级","平时成绩","平时成绩表现形式","平时成绩百分比","期中成绩","期中成绩表现形式","期中成绩百分比","期末成绩","期末成绩表现形式","期末成绩百分比"};
			List<Object[]> dataList =new ArrayList<Object[]>();
			Object[] objs=null;
			String percentageNormal = systemConfigservice.getvalue("pe_examination_normal")+"%";
			String percentageMid = systemConfigservice.getvalue("pe_examination_mid")+"%";
			String percentageScore = systemConfigservice.getvalue("pe_examination_end")+"%";
			for (int i = 0; i < data.size(); i++) {
				PeExaminationAppointment po=null;
				po=data.get(i);
				objs = new Object[rowsName.length];
				String[] xq=Semester.getnow().split("-");
				objs[0] = xq[0];
				objs[1] = Integer.parseInt(xq[2])==1?"第一学期":"第二学期";
				objs[2] = po.getUserStudentInfo().getUserStudentInfoNum();
				objs[3] = code;
				objs[4] = "必修课";
				objs[5]="";
				objs[6]="";
				objs[7]="";
				objs[8] = "初修";
				objs[9] = "考试";
				if(po.getPeExaminationAppointmentScore()==null)
					objs[12] = "缺考";
				else
					objs[12]=null;
				objs[14] = po.getPeExaminationTotalscore();
				objs[17] = po.getPeExaminationNormalscore();
				objs[19] = percentageNormal;
				objs[20] = po.getPeExaminationMidscore();
				objs[22] = percentageMid;
				objs[23] = po.getPeExaminationAppointmentScore();
				objs[25] = percentageScore;
				dataList.add(objs);
			}

			ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response);
			ex.exportData();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("/examinationscoreone.action")
	public String getexaminationscoreone(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		Page page=new Page(peExaminationScoreservice.countexaminationscore(1),pn);
		page.setPageSize(20);
		List<PeExaminationAppointment> data=peExaminationScoreservice.getexaminationscore(1,page);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("data", data);
		model.addAttribute("page",page);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("code", systemConfigservice.getvalue("sportsskillsone"));
		model.addAttribute("name", peExaminationservice.getskilltest("体育技能一"));
		return "examinationscoreone/examinationscoreone";
	}
	
	@RequestMapping("/selectexaminationscoreone.action")
	public String selectexaminationscoreone(@RequestParam("peExaminationId")Integer peExaminationId,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model,HttpSession session) {
		if(peExaminationId==0) {
			Page page=new Page(peExaminationScoreservice.countexaminationscore(1),pn);
			page.setPageSize(20);
			List<PeExaminationAppointment> data=peExaminationScoreservice.getexaminationscore(1,page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
		}else {
			Page page=new Page(peExaminationScoreservice.countselectexaminationscore(1,peExaminationId),pn);
			page.setPageSize(20);
			List<PeExaminationAppointment> data=peExaminationScoreservice.selectexaminationscore(peExaminationId,1,page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
		}
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("code", systemConfigservice.getvalue("sportsskillsone"));
		model.addAttribute("name", peExaminationservice.getskilltest("体育技能一"));
		session.setAttribute("peExaminationId",peExaminationId);
		return "examinationscoreone/examinationscoreone";
	}
	
	@RequestMapping("/searchexaminationscoreone.action")
	public String searchexaminationscoreone(@RequestParam("userStudentInfoNum")String userStudentInfoNum,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		List<PeExaminationAppointment> data=peExaminationScoreservice.searchexaminationscore(userStudentInfoNum, 1);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("data", data);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("code", systemConfigservice.getvalue("sportsskillsone"));
		model.addAttribute("name", peExaminationservice.getskilltest("体育技能一"));
		return "examinationscoreone/examinationscoreone";
	}

	@RequestMapping("/updatescore.action")
	@ResponseBody
	public Msg updatescore(@RequestParam("peExaminationAppointmentScore")double peExaminationAppointmentScore,@RequestParam("peExaminationAppointmentId")Integer peExaminationAppointmentId){
		try {
			peExaminationScoreservice.updatescore(peExaminationAppointmentScore,peExaminationAppointmentId);
			return Msg.success("操作成功");
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("操作失败");
		}
	}
	
	@RequestMapping("/export.action")
	public ModelAndView export(@RequestParam("userStudentInfoYear") Integer userStudentInfoYear,@RequestParam("userStudentInfoCollege") String userStudentInfoCollege,@RequestParam("name")Integer theoreticalExaminationId,HttpServletResponse response) {
		try {
			List<PeExaminationAppointment> data=peExaminationScoreservice.upselectexaminationscore(userStudentInfoYear, userStudentInfoCollege,theoreticalExaminationId,1);
			String code=systemConfigservice.getvalue("sportsskillsone");
			String title="体育技能考试成绩";
			String[] rowsName=new String[] {"取得学年","取得学期","学号","课程／环节代码","课程类别一","课程类别二","课程类别三","环节类别","修读性质","考核方式","辅修标记","补考标记","特殊情况标记","成绩表现形式","原始成绩"};
			List<Object[]> dataList =new ArrayList<Object[]>();
			Object[] objs=null;
			
			for (int i = 0; i < data.size(); i++) {  
				PeExaminationAppointment po =null;
						po=data.get(i);
                objs = new Object[rowsName.length];  
                String[] xq=Semester.getnow().split("-");
				objs[0] = xq[0];
				objs[1] = Integer.parseInt(xq[2])==1?"第一学期":"第二学期";
				objs[2] = po.getUserStudentInfo().getUserStudentInfoNum();
				objs[3] = code;
				objs[4] = "必修课";
				objs[5]="";
				objs[6]="";
				objs[7]="";
				objs[8] = "初修";
				objs[9] = "考试";
				if(po.getPeExaminationAppointmentScore()==null)
					objs[12] = "缺考";
				else
					objs[12]=null;
				objs[14] = po.getPeExaminationAppointmentScore();
                dataList.add(objs);  
            }  
			
            ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response);  
            ex.exportData();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/exportTotel.action")
	public ModelAndView exporttotel(HttpServletResponse response){
		try{
			List<PeExaminationAppointment> data=peExaminationScoreservice.getTotelScore(1);
			String[] rowsName=new String[] {"学年","考试名称","学号","姓名","入学时间","所在学院","课程","课程代码","考试成绩"};
			String title="体育技能考试成绩";
			List<Object[]> dataList =new ArrayList<Object[]>();
			Object[] objs=null;
			for (int i = 0; i < data.size(); i++) {
				PeExaminationAppointment po =null;
				po=data.get(i);
				objs = new Object[rowsName.length];
				objs[0] =Semester.getnow();
				objs[1] = po.getPeExamination().getPeExaminationName();
				objs[2] = po.getUserStudentInfo().getUserStudentInfoNum();
				objs[3] = po.getUserStudentInfo().getUserStudentInfoName();
				objs[4] = po.getUserStudentInfo().getUserStudentInfoYear();
				objs[5] = po.getUserStudentInfo().getUserStudentInfoCollege();
				objs[6] = "体育技能一";
				objs[7] = systemConfigservice.getvalue("sportsskillsone");
				objs[8] = po.getPeExaminationAppointmentScore()==null?"":""+po.getPeExaminationAppointmentScore();
				dataList.add(objs);
			}
			ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response);
			ex.exportData();
		}catch (Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
}
